<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\OrderAdminRemarkModel;
use think\App;
use think\facade\Db;

class OrderAdminRemark extends Base {

    protected $middleware = [Auth::class];
    private $model;
    private $req;

    private $page;

    private $limit;

    private $aid;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 999999;

        $this->model = new OrderAdminRemarkModel();

        $this->aid = $this->isLogin();
    }

    /*
     * 获取店铺列表
     * */
    public function getList(){

        if(empty($this->req['order_id'])){
            return $this->fail("订单id不能为空");
        }

        $where =[];

        $where["order_id"] = $this->req['order_id'];

        $list = $this->model->getList($where,$this->page,$this->limit,["created_at"=>"DESC"]);

        $total = $this->model->getTotal($where);

        return $this->succData($total,$list);

    }





    /*
     * 添加
     * */
    public function add()
    {

        if(empty($this->req['order_id'])){
            return $this->fail("订单id不能为空");
        }

        if(empty($this->req['content'])){
            return $this->fail("内容不能为空");
        }


        $res = $this->model->addOne([
            "order_id"=>$this->req["order_id"],
            "content"=>trim($this->req['content'])
        ]);

        if($res){
            return $this->succ();
        }else{
            return $this->fail();
        }
    }



    /*
       * 添加
       * */
    public function edit()
    {

        if(empty($this->req['id'])){
            return $this->fail("备注id不能为空");
        }

        if(empty($this->req['content'])){
            //return $this->succ();
        }


        $res = $this->model->updateOne(["id"=>$this->req['id']],[
            "content"=>trim($this->req['content']),
            "updated_at"=>date("Y-m-d H:i:s")
        ]);

        if($res){
            return $this->succ();
        }else{
            return $this->fail();
        }
    }

    public function del()
    {
        if(!isset($this->req['id']) || empty($this->req['id'])){
            return $this->fail("请选择要删除的备注");
        }

        $res = $this->model->where("id",$this->req['id'])->delete();

        if($res){
            return $this->succ();
        }else{
            return $this->fail();
        }
    }



}
